import tinymce from 'tinymce'
import lodash from 'lodash'
tinymce.PluginManager.add('wordlimit', function (editor) {
  var pluginName = '字数限制';
  var app = tinymce.util.Tools.resolve('tinymce.util.Delay');
  var Tools = tinymce.util.Tools.resolve('tinymce.util.Tools');
  var wordlimit_event = editor.getParam('ax_wordlimit_event', 'SetContent Undo Redo Keyup input paste');
  var options = editor.getParam('wordlimit', {}, 'object');
  // 默认配置
  var defaults = {
    max: null, // 最多可以输入多少字
    spaces: true, // 是否含空格
    isInput: false, // 是否在超出后还可以输入 
    maxMessage: "超出最大输入字符数量！",
    changeCallback: () => { }, // 自定义的回调方法 
    changeMaxCallback: false,
  };

  class WordLimit {
    constructor(editor, options) {
      options = Tools.extend(defaults, options);
      let preCount = 0;
      let _wordCount = 0;
      let oldContent = editor.getContent();
      let WordCount = editor.plugins.wordcount;
      let sumLetter = function (html) {
        var re1 = new RegExp("<.+?>", "g");
        var txt = html.replace(re1, '');
        txt = txt.replace(/\n/g, '');
        txt = txt.replace(/&nbsp;/g, ' ');
        return { txt: txt, html }
      }
      if (options.changeMaxCallback) {
        options.changeMaxCallback = lodash.debounce(options.changeMaxCallback, 2000, {leading:true, trailing:false})
      }
      editor.on(wordlimit_event, function (e) {
        var content = editor.getContent() || e.content || '';
        if (options.spaces) {
          // 字数
          _wordCount = WordCount.body.getCharacterCount();
        } else {
          // 不含空格字数
          _wordCount = WordCount.body.getCharacterCountWithoutSpaces();
        }
        options.changeCallback({ ...options, editor, num: _wordCount, content, ...sumLetter(content) })
        if (_wordCount > options.max) {
          preCount = _wordCount;
          // 禁止再输入
          if (options.isInput !== 1) {
            // 内容超出还原
            editor.setContent(oldContent);
            // 还原后重新统计
            if (options.spaces) {
              _wordCount = WordCount.body.getCharacterCount();
            } else {
              _wordCount = WordCount.body.getCharacterCountWithoutSpaces();
            }
          }
          editor.getBody().blur();
          editor.fire('wordlimit', {
            maxCount: options.max,
            wordCount: _wordCount,
            preCount: preCount,
            isPaste: (e.type === 'paste' || e.paste) || false
          });
          if (options.changeMaxCallback) {
            options.changeMaxCallback({
              maxCount: options.max,
              wordCount: _wordCount,
              preCount: preCount,
              isPaste: (e.type === 'paste' || e.paste) || false
            });
          } else {
            var close = null;
            var toast = function (message) {
              close && close.close();
              close = editor.notificationManager.open({
                text: message,
                type: 'error',
                timeout: 3000,
              });
              return
            }
            toast(options.maxMessage)
          }
        }
        oldContent = editor.getContent();
      });
    }
  }
  var setup = function () {
    // 如果没设置最大输入限制，则不进行下一步操作
    if (!options && !options.max) return false;
    // 如果没有配置字数统计，则不进行下一步，并且提示配置字数统计
    if (!editor.plugins.wordcount) {
      console.error('请先在tinymce的plugins配置wordlimit之前加入wordcount插件');
      return false;
    }
    // app.setEditorTimeout(editor, function () {}, 300)
    editor.on('init', function () {
      if (!editor.settings.wordlimit || !editor.settings.wordlimit.max) {
        return;
      }
      let editDom = editor.getContainer();
      let wordNum = editDom.querySelector("button.tox-statusbar__wordcount");
      if (wordNum?.innerText?.indexOf("字符") === -1) wordNum.click();
      new WordLimit(editor, {...options, ...editor.settings.wordlimit})
    });
  }
  setup();
  return {
    getMetadata: function () {
      return { name: pluginName };
    }
  };
});